/*
*********************************
	SF2d Game Engine
*********************************

	By Julian Williams
	June 11, 2010
**********************************

	Description:

		Screen Fade Class used
	 to fade screen in/out
***********************************
*/

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

#include "app.h"
#include "ScreenFade.h"

ScreenFade ScreenFade::_instance;

int fSpeed;

//Instance of singleton screenfade
ScreenFade &ScreenFade::getInstance()
{
	return _instance;
}



ScreenFade::ScreenFade()
{
	faderSpeed = 500;
	fadeColor = sf::Color(0,0,0);

	
	//create blank image
	fade_screen.Create( 1024, 800, fadeColor); 


	SetImage(fade_screen);
	//Resize(App::Get_RenderWindow()->GetWidth(),  App::Get_RenderWindow()->GetHeight());

}



void ScreenFade::Fade_Out()
{
	//Copy the screen to this image
	currentScreen_img.CopyScreen(*App::getInstance().Get_RenderWindow(), sf::IntRect(0, 0, App::getInstance().Get_ScreenSize().x, App::getInstance().Get_ScreenSize().y));
	currentScreen.SetImage(currentScreen_img);
	//cout << "What the FUCK is: " << App::getInstance().Get_ScreenSize().x << " and " << App::getInstance().Get_ScreenSize().y << endl;
	
	
	fSpeed = faderSpeed * App::getInstance().Get_RenderWindow()->GetFrameTime();
	if(fSpeed <= 0) { fSpeed = 5; }

	for(int i = 0; i < 255; i += fSpeed)
	{
		App::getInstance().Get_RenderWindow()->Clear();

		//Draw current screen
			App::getInstance().Get_RenderWindow()->Draw(currentScreen);

		//Set color and alpha channel
			SetColor(sf::Color(fadeColor.r, fadeColor.g, fadeColor.b, i));
	
		//Draw fade image
			App::getInstance().Get_RenderWindow()->Draw(getInstance());
		

		// Update the window
		App::getInstance().Get_RenderWindow()->Display();
	}
	
}//End ov function



//Fade in
void ScreenFade::Fade_In()
{
	
	App::getInstance().Draw_Scene();
	//Copy the screen to this image
	currentScreen_img.CopyScreen(*App::getInstance().Get_RenderWindow(), sf::IntRect(0, 0, App::getInstance().Get_ScreenSize().x, App::getInstance().Get_ScreenSize().y));
	currentScreen.SetImage(currentScreen_img);
	//cout << "What the FUCK is: " << App::getInstance().Get_ScreenSize().x << " and " << App::getInstance().Get_ScreenSize().y << endl;

	//fSpeed *= App::getInstance().Get_RenderWindow()->GetFrameTime();
	if(fSpeed <= 0) { fSpeed = 5; }

	for(int i = 255; i > 0; i -= fSpeed)
	{
		App::getInstance().Get_RenderWindow()->Clear();

		App::getInstance().Draw_Scene();
		//Draw current screen
			//App::getInstance().Get_RenderWindow()->Draw(currentScreen);

		//Set color and alpha channel
			SetColor(sf::Color(fadeColor.r, fadeColor.g, fadeColor.b, i));
	
		//Draw fade image
			App::getInstance().Get_RenderWindow()->Draw(getInstance());
		

		// Update the window
		App::getInstance().Get_RenderWindow()->Display();
	}
	
}//End ov function



void ScreenFade::Set_Color(sf::Color newColor)
{
	fadeColor = newColor;
}

